package com.itheima.controller;

import com.itheima.common.PageBean;
import com.itheima.common.R;
import com.itheima.dto.QueryClazzDto;
import com.itheima.pojo.Clazz;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    /**
     * 新增班级
     */
    @PostMapping
    public R save(@RequestBody Clazz clazz) {
        clazzService.save(clazz);
        return R.ok();
    }

    /**
     * 条件分页查询班级
     */
    @GetMapping
    public R page(QueryClazzDto dto) {
        PageBean pageBean = clazzService.page(dto);
        return R.ok(pageBean);
    }

    /**
     * 根据ID查询班级详情
     */
    @GetMapping("/{id}")
    public R getInfo(@PathVariable Integer id) {
        Clazz clazz = clazzService.getInfo(id);
        return R.ok(clazz);
    }

    /**
     * 更新班级信息
     */
    @PutMapping
    public R update(@RequestBody Clazz clazz) {
        clazzService.update(clazz);
        return R.ok();
    }

    /**
     * 根据ID删除班级
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id) {
        clazzService.deleteById(id);
        return R.ok();
    }

    /**
     * 查询全部班级
     */
    @GetMapping("/list")
    public R findAll() {
        List<Clazz> clazzList = clazzService.findAll();
        return R.ok(clazzList);
    }
}
